package com.acme;

import java.io.IOException;

import com.acme.service.bank.*;
import com.acme.service.network.BankServer;
import com.acme.domain.bank.Bank;
import com.acme.exceptions.ClientAlreadyExistsException;
import com.acme.exceptions.ClientCountLimitException;
import com.acme.exceptions.ConnectionLostException;
import com.acme.exceptions.ServerAlreadyRunningException;

class BankApplication {
	public static void main(String[] args) throws IOException, ClientAlreadyExistsException, ClientCountLimitException{
		String path = null;
		boolean loadfeedFlag = false;
		boolean serverFlag = false;
		for (int i=0; i<args.length; i++) {
			if ("-loadfeed".equals(args[i]) ) {
				loadfeedFlag = true;
				try {
					path = args[i+1];
				} catch (ArrayIndexOutOfBoundsException e) {
					throw new IOException("wrong command line input! (missing path after loadfeed");
				}
			} else if ("-server".equals(args[i])){
				serverFlag = true;
			}
			
		}
		Bank db = new Bank();
		
		if (loadfeedFlag){
			BankService.loadBank(db, path);
		}
		
		if (serverFlag){
			try {
				BankServer.startServer(db);
			} catch (ServerAlreadyRunningException | ConnectionLostException e) {
				System.out.println(e.getMessage());
			}
		}
	}
}